//
//  WJBFlowViewLayout.swift
//  WJBProject
//
//  Created by 无尽宝 on 2020/4/13.
//  Copyright © 2020 VolantPig. All rights reserved.
//

import UIKit

protocol WJBFlowViewLayoutDelegate: NSObjectProtocol {
    func collectionViewSizeForItemAtIndexPath(collectionView: UICollectionView, collectionViewLayout: UICollectionViewLayout, indexPath: IndexPath) -> CGSize
}

// item 流式布局
class WJBFlowViewLayout: UICollectionViewFlowLayout {
    
    private var attArray = [UICollectionViewLayoutAttributes]()
    private var contentSize: CGSize = .zero
    
    weak var myDelegate: WJBFlowViewLayoutDelegate?
    /// 距离上下左右的距离
    var sectionInsets: UIEdgeInsets = UIEdgeInsets.zero
    
    /// 上下两个item的距离
    var lineSpacing: CGFloat = 10
    /// 左右两个item的距离
    var interitemSpacing: CGFloat = 10
    
    // 整个section的高度，相当于collectionView的高度了，注意，使用前记得layoutIfNeeded()
    /// collectionView的section的高度
    var sectionHeight: CGFloat = 0
    
    override func prepare() {
        super.prepare()
        
        self.myDelegate = self.collectionView?.delegate as! WJBFlowViewLayoutDelegate
        
        var offsetX = self.sectionInsets.left
        var offsetY = self.sectionInsets.top
        //获取指定组的cell的个数
        let itemsCount = self.collectionView?.numberOfItems(inSection: 0) ?? 0
        
        //collectionView的宽
        let collectionViewWidth = self.collectionView?.frame.size.width ?? 0
        
        for index in 0..<itemsCount {
            let indexPath = IndexPath.init(row: index, section: 0)
            let size = self.myDelegate?.collectionViewSizeForItemAtIndexPath(collectionView: self.collectionView!, collectionViewLayout: self, indexPath: indexPath)
            let maxX = offsetX + (size?.width ?? 0)
            if maxX > (collectionViewWidth - self.sectionInsets.right) {
                offsetX = self.sectionInsets.left
                offsetY += (size?.height ?? 0) + self.lineSpacing
            }
            let attributes = UICollectionViewLayoutAttributes.init(forCellWith: indexPath)
            attributes.frame = CGRect.init(x: offsetX, y: offsetY, width: size?.width ?? 0, height: size?.height ?? 0)
            self.attArray.append(attributes)
            
            offsetX += (size?.width ?? 0) + self.interitemSpacing
            
            self.contentSize.height = offsetY + (size?.height ?? 0) + self.sectionInsets.bottom
            self.sectionHeight = offsetY + (size?.height ?? 0) + self.sectionInsets.bottom
        }
        self.contentSize.width = collectionViewWidth
        
    }
    
//    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
//        return self.attArray
//    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var array = [UICollectionViewLayoutAttributes]()
        _ = attArray.map({
            if rect.contains($0.frame) {
                array.append($0)
            }
        })
        return array
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return self.attArray[indexPath.item]
    }
    
    override var collectionViewContentSize: CGSize {
        get {
            return self.contentSize
        }
    }
    
}
